package lumis.service.wsrp.producer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.NoResultException;

import lumis.doui.source.AbstractObjectDataProvider;
import lumis.doui.source.IDataProvider;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.util.ITransaction;
/**
/**
 * {@link IDataProvider} responsible for populating a {@link TabularData} with
 * the {@link RemoteConsumerRegistration} associated with the current producer.
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
@SuppressWarnings("unchecked")
public class RegistrationManagementInterfaceDataProvider extends AbstractObjectDataProvider<TabularSource, RemoteConsumerRegistration>
{
	
	/**
	 * Default constructor
	 * @since 6.0.0
	 */
	public RegistrationManagementInterfaceDataProvider()
	{
		super(RemoteConsumerRegistration.class);
	}
	
	@Override
	protected List<RemoteConsumerRegistration> loadObjects(SessionConfig sessionConfig, TabularSource source, ITransaction transaction) throws PortalException
	{
		String serviceInstanceId = source.getDouiContext().getRequest().getServiceInstanceConfig().getId();
		String registrationHandle = (String) source.getParameterValue("registrationHandle");

		if(registrationHandle == null)
		{
			// get the producer associated with current service instance
			ProducerImpl producer;
			try
			{
		
					producer = (ProducerImpl) ManagerFactory.getEntityManager()
													.createNamedQuery(ProducerImpl.NAMED_QUERY_FIND_PRODUCER_BY_NAMESPACE)
													.setParameter("namespace", serviceInstanceId)
													.getSingleResult();
			}
			catch (NoResultException e)
			{
				producer = null;
			}
			
			if(producer == null)
				return Collections.emptyList();
			
			return new ArrayList<RemoteConsumerRegistration>(producer.getRegistrations());
		}
		else
		{
			RemoteConsumerRegistration registration = ManagerFactory.getEntityManager().find(RemoteConsumerRegistration.class, registrationHandle);
			if(registration == null)
				return Collections.emptyList();
			return Collections.singletonList(registration);
		}
		
		
	}
	
	@Override
	protected Object readProperty(RemoteConsumerRegistration object, String propertyName) throws UnexpectedException
	{
		if("numberClonedPortlets".equals(propertyName)){
			if(object.getClonedPortlets() != null)
			{
				return object.getClonedPortlets().size();
			}
			else
			{
				return 0;
			} 
		}
		return super.readProperty(object, propertyName);
	}
	
	@Override
	protected int getTotalRows(SessionConfig sessionConfig, TabularSource source, ITransaction transaction) throws PortalException
	{
		return source.getData().size();
	}
}
